package com.action;

import java.util.List;

import com.hdb.DB;
import com.hdb.TClassification;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;

@ParentPackage("default")
@Namespace("/class")
@Results({
        @Result(location = "/admin/classification/classManage.jsp"),
        @Result(name = "succeed", location = "/common/succeed.jsp"),
        @Result(name = "edit", location = "/admin/classification/classEditPage.jsp"),
        @Result(name = "classAll", location = "/view/class/classAll.jsp")
})
@InterceptorRef("adminStack")/*设置过滤器*/
public class AdminClassAction extends BasicAction {
    private int classId;
    private String className;
    private String classInfo;

    private String message;
    private String path;

    private DB db;

    /*
     * 查看当前系统中的点卡类别
     */
    @Action("classManage")
    public String classManage() {
        List classificationList = db.classDAO.findAll(0, 20);
        getRequest().put("classList", classificationList);
        return ActionSupport.SUCCESS;
    }


    /*
     * 查看当前系统中的所有点卡类别
     */
    @Action("classAll")
    public String classAll() {
        List classificationList = db.classDAO.findAll(0, 20);
        getRequest().put("classList", classificationList);
        return ActionSupport.SUCCESS;
    }

    /*
     * 添加点卡种类，对结果给出提示
     */
    @Action("classAdd")
    public String classAdd() {
        TClassification classification = new TClassification();
        classification.setClassName(className);
        classification.setClassInfo(classInfo);
        this.setMessage(getResultInfo(
                db.classDAO.save(classification)
        ));
        this.setPath("class/classManage.action");
        return "succeed";
    }

    /*
     * 删除某一点卡类别
     * 如果类别下存在商品，给出提示
     */
    @Action("classDel")
    public String classDel() {
        List goodClassList = db.goodDAO.find("classId", classId, 0, 20);
        if (goodClassList.size() > 0) {
            this.setMessage("请先删除此类别下的商品");
        } else {
            TClassification classification = db.classDAO.findById(classId);
            db.classDAO.delete(classification);
            this.setMessage("操作成功");
        }
        this.setPath("class/classManage.action");
        return "succeed";
    }
    /*类别编辑页面*/
    @Action("classEditPage")
    public String classEditPage() {
        TClassification classification = db.classDAO.findById(classId);
        getRequest().put("clazz", classification);
        return "edit";
    }

    /*
     * 修改商品类别信息
     * 给出处理结果
     */
    @Action("classEdit")
    public String classEdit() {
        TClassification classification = db.classDAO.findById(classId);
        classification.setClassName(className);
        classification.setClassInfo(classInfo);
        db.classDAO.attach(classification);
        this.setMessage("操作成功");
        this.setPath("class/classManage.action");
        return "succeed";
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getClassInfo() {
        return classInfo;
    }

    public void setClassInfo(String classInfo) {
        this.classInfo = classInfo;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }
}
